const User = require('../model/user.model')
class UserService {
    async createUser(userName, passWord) {
        const res = await User.create({ userName, passWord })
        console.log(res)
        return res
    }
    async getUserInfo({ id, userName, passWord, isAdmin }) {
        const whereOpt = {}
        id && Object.assign(whereOpt, { id })
        userName && Object.assign(whereOpt, { userName })
        passWord && Object.assign(whereOpt, { passWord })
        isAdmin && Object.assign(whereOpt, { isAdmin })
        const res = await User.findOne({
            attributes: [ 'id', 'userName', 'passWord', 'isAdmin' ],
            where: whereOpt
        })
        return res ? res.dataValues : null
    }
    async updateById({ id, userName, passWord, isAdmin }) {
        const whereOpt = { id }
        const newUser = {}
        userName && Object.assign(newUser,{userName})
        passWord && Object.assign(newUser,{passWord})
        isAdmin && Object.assign(newUser, {isAdmin})
        const res = await User.update(newUser, { where: whereOpt })
        return res[0] > 0
    }
}
module.exports = new UserService()